layui.use(['form','table','element','laytpl','layer','upload'], function() {
  var table = layui.table;
  var upload = layui.upload;
  table.render({
    elem: '#supplierTable'
    ,url:'/ssm/banner/queryList.do'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,page : true
    ,height : "full-125"
    ,cols: [[
      {field:'id', width:80, title: 'ID',type:"numbers"}
      ,{field:'title', title: '广告图标题'}
      ,{field:'link', title: '外链地址'}
      ,{field:'sort', title: '排序'}
      ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
    ]]
  });
  

  //工具条点击事件
  	layui.table.on('tool(table)', function(obj){
  		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'del'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data.id);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		}
  	});


  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  	$(".search_btn").on("click", function() {
        var queryValue=$("[name=queryValue]").val().trim();
        var queryKey=$("[name=queryKey]").val().trim();
        var postData={
        		title:'',
        		code:'',
        } //创建键值对象
        postData[queryKey]=queryValue;
        table.reload("supplierTable", {
            page : {
                curr : 1
            },
            where : postData
        })
    });

  	//显示表单弹窗
    function showEditModel(data){
        layer.open({
            type: 1,
            title: data==null?"添加":"修改",
            area: '90%',
            offset: 'auto',
            content: $("#addModel").html()
        });
        if(data!=null){
            $("#editForm input[id=id]").val(data.id);
            $("#editForm input[id=title]").val(data.title);
            $("#editForm input[id=link]").val(data.link);
            $("#editForm input[id=sort]").val(data.sort);
            $("#editForm input[id=banner_path]").val(data.imgUrl);
            $('#demo2').append('<img src="'+ data.imgUrl +'" alt="" class="layui-upload-img">')
        }
        $("#btnCancel").click(function(){
            layer.closeAll('page');
        });
        upload.render({
    	    elem: '#test2'
    	    ,url: '/ssm/file/uploadImage.do' //改成您自己的上传接口
    	    ,multiple: false
    	    ,before: function(obj){
    	      //预读本地文件示例，不支持ie8
    	      $('#demo2').empty();
    	      $("#editForm input[id=banner_path]").val("");
    	      obj.preview(function(index, file, result){
    	        $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
    	      });
    	    }
    	    ,done: function(res){
    	      //上传完毕
    	        $("#editForm input[id=banner_path]").val($("#editForm input[id=banner_path]").val()+res.body);
    	    }
    	  });

    }

    	//表单提交事件
    layui.form.on('submit(btnSubmit)', function(data){
            var qUrl="/ssm/banner/save.do";
            $.post(qUrl,{
                id : $("#id").val(),
                title : $("#title").val(),
                link : $("#link").val(),
                sort : $("#sort").val(),
                imgUrl : $("#banner_path").val(),
            },function(res){
             if(res.code=="01"){
                 layer.closeAll();
                 layer.msg("保存成功");
                 //刷新父页面
                 layui.table.reload('supplierTable', {});
             }else{
                 layer.msg("保存失败");
             }
         });
            return false;
    });

    
	//删除
	function doDelete(data){
        layer.confirm('确定删除吗？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/banner/del.do",{
            		id : data.cid
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable', {});
                    
                }else{
                	layer.close(index);
                    layer.alert(data.body);
                    
                }

            })
        });
	}

});